<template>
  <h1 style="text-align: center">成人疫苗接种登记</h1>
  <h2 style="text-align:left;color:red">温馨提示：我院开展九价HPV疫苗、23价肺炎疫苗、20ug重组乙肝疫苗、四价流感疫苗</h2>
  <h3 style="text-align:left;color:red">温馨提示：预约成功后凭借短信要求按时接种</h3>
  <el-form v-loading="loading" :model="vaccine" ref="vaccineRef" label-width="120px" :rules ="vaccineRule" label-position="top">
    <el-form-item label="姓名" prop="userName">
      <el-input v-model="vaccine.userName" />
    </el-form-item>
    <el-form-item label="身份证" prop="certificateCode">
      <el-input v-model="vaccine.certificateCode" />
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-select v-model="vaccine.sex" placeholder="" name="sex">
        <el-option :label="$t('msg.userInfo.male')" value="0" />
        <el-option :label="$t('msg.userInfo.female')" value="1" />
      </el-select>
      <!-- <el-input v-model="vaccine.sex" /> -->
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input type="number" v-model="vaccine.age" />
    </el-form-item>
    <el-form-item label="地区" prop="area">
      <el-input v-model="vaccine.area" />
    </el-form-item>
    <el-form-item label="疫苗" prop="vaccine">
      <el-select v-model="vaccine.vaccine" placeholder="" name="vaccine">
        <el-option key="0" label="九价HPV疫苗(16-45岁)" value="0"/>
        <!-- <el-option key="1" label="四价HPV疫苗(26-45岁)" value="1" />
        <el-option key="2" label="23价肺炎疫苗(18岁及以上)" value="2"/>
        <el-option key="3" label="20ug重组乙肝疫苗(18岁及以上)" value="3" />
        <el-option key="4" label="四价流感疫苗(18岁及以上)" value="4"/> -->
      </el-select>
    </el-form-item>
    <el-form-item label="联系方式" prop="telNo">
      <el-input v-model="vaccine.telNo">
        <template #append>
          <el-button type="info" :icon="Message" circle @click="sendValidCode">获取验证码</el-button>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="验证码" prop="validCode">
      <el-input v-model="vaccine.validCode" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="register">{{$t('msg.common.register')}}</el-button>
    </el-form-item>
  </el-form>
  <div>
    <!--提示-->
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { apply } from '@/api/single/vaccine'
import { sendSmsCode } from '@/api/common'
import { validateTel, validateIdentityCard } from '@/utils/validate'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const route = useRoute()
const vaccine = ref({
  vaccineName: '',
  fileDesc: '',
  vaccineOwnerName: '',
  tel: ''
})

const i18n = useI18n()

const vaccineRef = ref(null)

const vaccineRule = ref({
  userName: [
    {
      required: true,
      trigger: 'blur',
      message: '请填写姓名'
    }
  ],
  certificateCode: [
    {
      required: true,
      trigger: 'blur',
      validator: validateIdentityCard()
    }
  ],
  sex: [
    {
      required: true,
      trigger: 'blur',
      message: '性别不能为空'
    }
  ],
  age: [
    {
      required: true,
      trigger: 'blur',
      message: '年龄不能为空'
    }
  ],
  area: [
    {
      required: true,
      trigger: 'blur',
      message: '区域不能为空'
    }
  ],
  vaccine: [
    {
      required: true,
      trigger: 'change',
      message: '疫苗需要选择，不能为空'
    }
  ],
  telNo: [
    {
      required: true,
      trigger: 'blur',
      message: '电话号码错误',
      validator: validateTel()
    }
  ],
  validCode: [
    {
      required: true,
      trigger: 'blur',
      message: '未填写验证码'
    }
  ]
})

const register = async () => {
  vaccineRef.value.validate(valid => {
    if (!valid) return
    loading.value = true
    apply(vaccine.value).then(res => {
      loading.value = false
      ElMessage.success(i18n.t('msg.common.success') || res.message)
      vaccine.value = {}
    }).catch(err => {
      ElMessage.error(i18n.t('msg.common.error') || err.message)
      loading.value = false
    })
  })
}

const sendValidCode = () => {
  if (vaccine.value.telNo) {
    sendSmsCode(vaccine.value.telNo).then(res => {
      ElMessage.success('验证码发送成功')
    }).catch(err => {
      ElMessage.error(err.message)
    })
  } else {
    ElMessage.error('请填写收手机号码')
  }
}
</script>

<style scope>
#app {
    height: 100%;
    width: 90%;
    position: absolute;
    left: 5%;
}
</style>
